package oop.ex2.expression;

import oop.ex2.exceptions.UnrecognizedVariable;

/**
 * Implements the base two logarithmic function.
 * @author daniel_i_l
 *
 */
public class LogFunctionNode extends FunctionNode {
	private static final int NUMBER_OF_PARAMS = 1;
	private static boolean BOUNDED_PARAMETERS = true;
	private static final double BASE_TWO = 2.0;
	
	/**
	 * Build a function node.
	 * @param matchedString The string that caused this function to be created.
	 */
	public LogFunctionNode(String matchedString) {
		super(matchedString, NUMBER_OF_PARAMS, BOUNDED_PARAMETERS);
	}

	@Override
	public double calculateNode() throws UnrecognizedVariable {
		
		double value = _children.get(0).calculateNode();
		
		if (value <= 0)
			throw new ArithmeticException("Log only excepts positive numbers");
		return (double) (Math.log(_children.get(0).calculateNode()) / Math.log(BASE_TWO));
	}

}
